using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineGame;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
namespace WarsOnRiceFields
{
    public class Items
    {
        int x;
        int y;
        int xPre;
        int yPre;
        bool isChoose = false;
        string name;
        string content;
        string pathImage;
        Texture2D image;
        Rectangle rect;
        Rectangle textarea;
        float scale;
        SpriteFont font;
        private Sprite circleItem;
        public bool isInList = false;
        
        public int currentTimeLoad;
        public int timeLoad;

        public Items(string _name, string _content, string path, int Fx ,int position)
        {
            name = _name;
            content = _content;
            pathImage = path;
            x = Fx + 110 * (position%2);
            y = 80+ 80 * (position/2);
            xPre = x;
            yPre = y;
            rect = new Rectangle(x, y, 80, 80);
            scale = 0.8f;
            timeLoad = 10000;
            currentTimeLoad = 0;
        }
        public void setLocation(int _x, int _y)
        {
            x = _x;
            y = _y;
            rect = new Rectangle(x, y, 80, 80);
        }
        public void resetLocation()
        {
            x = xPre;
            y = yPre;
            rect = new Rectangle(x, y, 80, 80);
        }
        public void LoadContent(ContentManager content, SpriteFont _font)
        {
            image=content.Load<Texture2D>(pathImage);
            font = _font;
            textarea = new Rectangle(280,220,230,80);
            circleItem = new Sprite(content, "Items/iCirle");
        }
        private string parseText(string text)
        {
            string line = null;
            string returnline = null;
            string[] wordArray = text.Split(' ');
            foreach (string word in wordArray)
            {
                if (font.MeasureString(line + word).Length() > textarea.Width)
                {
                    returnline = returnline + line + '\n';
                    line = null;
                }
                line = line + word + ' ';
            }
            return returnline + line;
        }
        public void Update(GameTime gameTime)
        {
            this.currentTimeLoad += gameTime.ElapsedGameTime.Milliseconds;
        }
        public void Draw(SpriteBatch sprite)
        {
            if (isChoose)
            {
                
                sprite.Draw(image, new Vector2(x, y), null, Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                sprite.Draw(image, new Vector2(360,120), Color.White);
                sprite.DrawString(font, name, new Vector2(340, 80), Color.Red);
                sprite.DrawString(font, parseText(content), new Vector2(300, 240), Color.Red);
            }
            else
            {
                sprite.Draw(image, rect, Color.White);
            }
        }
        public bool OnSelected(TouchLocation touch)
        {
            if (rect.Contains((int)touch.Position.X, (int)touch.Position.Y))
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    isChoose = true;
                    return false;
                }
                else
                    if (touch.State == TouchLocationState.Released)
                    {
                        isChoose = false;
                        return true;
                    }
            }
            else isChoose = false;
            return false;
        }
    }
}
